<?php
/**
 * 名称：PluginAction
 * 描述：后台插件管理
 * 作者：lan 时间：2013-05-02
 * 版本号：V1.0.0
 * ========================历史版本=================================
 * 序号	 版本号 	日期			姓名	 	描述
 * 1 	V1.0.0 	2013-05-02 	lan  	后台插件管理
 * ========================历史版本=================================
 * 说明：请在有更改程序的时候修改以上注释
 */
class PluginAction extends CommonAction {
	/**
	 * 已安装插件
	 * Create Data <2013-05-02>
	 */
	public function installPlugin() {
		$list = M ( 'Addons' )->where ( "status='1'" )->select ();
		$this->assign ( 'list', $list );
		$this->display ();
	}
	
	/**
	 * 待安装插件
	 * Create Data <2013-05-02>
	 */
	public function waitInstallPlugin() {
		$addons = M ( 'Addons' );
		$list = $addons->where("status='0'")->select();
		$this->assign('list',$list);
		$this->assign('waitInstall',$this->_getPlugInfo ());
		$this->display();
	}
	
	/**
	 * 获取数据库需要启用的插件
	 * Create Data <2013-05-02>
	 */
	private function _getPlugInfo($dname = '') {
		$addons = M ( 'Addons' );
		$dirName = getcwd () . '/Plugins/';
		$dir = dir ( $dirName );
		$fileAddons = array ();
		while ( false !== ($entry = $dir->read ()) ) {
			if ($entry == '.' || $entry == '..' || $entry == ".svn") {
				continue;
			}
			if(empty($dname)) {
				if($addons->where("name='$entry'")->find()) {
					continue;
				}
				$path = $dirName . DIRECTORY_SEPARATOR . $entry;
				$addonsFile = $path . DIRECTORY_SEPARATOR . $entry . 'Actions.class.php';
				if (is_file ( $addonsFile )) {
					require_once $addonsFile;
					$class = $entry . 'Actions';
					$fileAddons [$entry]['file'] = new $class ();
				} 
			} else {
				$path = $dirName . DIRECTORY_SEPARATOR . $dname;
				$addonsFile = $path . DIRECTORY_SEPARATOR . $dname . 'Actions.class.php';
				if (is_file ( $addonsFile )) {
					require_once $addonsFile;
					$class = $dname . 'Actions';
					$fileAddons [$dname]['file'] = new $class ();
				}
			}
		}
		return $fileAddons;
	}
	
	/**
	 * 安装插件
	 * Create Data <2013-05-02>
	 */
	public function install() {
		$addons = M ( 'Addons' );
		$key = $_GET['k'];
		if($addons->where("name='$key'")->find()) {
			$this->error(L('alreadyInstalled'));
		} else {
			$list = $this->_getPlugInfo ( $key );
			$c = $this->getPlugFile($key);
			foreach ( $list as $k => $v) {
				foreach ($v as $v1) {
					$data ['name'] = $key;
					$data ['pluginName'] = $v1->pluginName;
					$data ['author'] = $v1->author;
					$data ['info'] = $v1->info;
					$data ['version'] = $v1->version;
					//$data ['status'] = '1';
					$data ['site'] = $v1->site;
					$data ['lastupdate'] = time();
					$data ['isMenu'] = $v1->isMenu;
					$data ['ylVersion'] = $v1->ylVersion;
					
					if ($addons->add ( $data ) && $c->install()) {
					 	$this->success ( L('installSucceed'));
					} else {
						$this->error ( L('installFailed') );
					}
				}
			}
		}
	}
	
	/**
	 * 插件卸载
	 * Create Data <2013-05-02>
	 */
	public function uninstall() {
		$key = $_GET['k'];
		$c = $this->getPlugFile($key);
		$c->uninstall();
		if(M('Addons')->where("name='$key'")->delete()) {
			$this->success(L('uninstalledPlugin'));
		} else {
			$this->error(L('uninstallPluginFailed'));
		}
	}
	
	/**
	 * 插件停用
	 * Create Data <2013-05-02>
	 */
	public function stop() {
		$key = $_GET['k'];
		$data['status'] = '0';
		if(M('Addons')->where("name='$key'")->save($data)) {
			$this->success(L('disabledPlugin'));
		} else {
			$this->error(L('disabledPluginFailed'));
		}
	}
	
	/**
	 * 插件启动
	 * Create Data <2013-05-02>
	 */
	public function start() {
		$key = $_GET['k'];
		$data['status'] = '1';
		if(M('Addons')->where("name='$key'")->save($data)) {
			$this->success(L('pluginStartupSuccess'));
		} else {
			$this->error(L(''));
		}
	}
	
	/**
	 * 获取插件
	 * Create Data <2013-05-02>
	 */
	public function getPlugFile($name) {
		$dirName = getcwd () . '/Plugins/';
		$dirName = $dirName.$name.DIRECTORY_SEPARATOR.$name.C('App_PLUGIN_SUFFIX').'.class.php';
		if(is_file($dirName)) {
			if(file_exists($dirName)) {
				require_once $dirName;
				$class = $name . 'Actions';
				if(class_exists($class)) {
					$c = new $class($this);
				}
			}
		}
		return $c;
	}
	
	/**
	 * 获取后台管理面板
	 * Create Data <2013-05-02>
	 */
	public function adminMenu() {
		$name = $_GET['name'];
		$dirName = $this->getPlugFile($name);
		$dirName->menu();
	}
	
	/**
	 * 加载插件
	 * Create Data <2013-05-02>
	 */
	public function adminManagers() {
		PluginManager::adminManger($_GET['addonsName'],$_GET['name'],$_GET['param']);
	}
}
?>